package com.yoomiito.app.ui.order.list;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.bean.ExamineBean;
import com.yoomiito.app.model.bean.ExamineBean1;
import com.yoomiito.app.model.bean.OrderInfo;
import com.yoomiito.app.model.bean.OrderList;
import com.yoomiito.app.model.order.OrderGoods;
import com.yoomiito.app.ui.order.detail.OrderInfoActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.widget.SpacingDecoration;
import com.yoomiito.app.widget.dialog.AccpetDialog;
import com.yoomiito.app.widget.dialog.RejectDialog;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.LoadingType;
import com.yoomiito.app.widget.state.StatusManager;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import kotlin.Unit;

public class OrderFragment2 extends BaseLazyFragment<OrderFmPresenter2> {
    @BindView(R.id.fm_rcy)
    RecyclerView mRecyclerView;
    @BindView(R.id.fm_refreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    int currentPage = 1;
    private int mType = AllCode.TaoBao;
    private OrderAdapter orderAdapter;
    private int key;
    private boolean isRefresh;

    private RejectDialog rejectDialog;
    private AccpetDialog accpetDialog;

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public View getLayoutView() {
        mStatusManager = new StatusManager.Builder(context)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new LoadingType())
                .addType(new EmptyType().setEmptyContent(""))
                .build();
        return mStatusManager.view();
    }


    @Override
    public OrderFmPresenter2 newP() {
        return new OrderFmPresenter2(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mType = bundle.getInt(Constant.TYPE);
        key = bundle.getInt(Constant.KEY);   // 2 客户

//        switch (type) {
//            case AllCode.TaoBao:
//                mType = 1;
//                break;
//            case AllCode.Gift:
//                mType = 2;
//                break;
//            case AllCode.BaoPin:
//                mType = 3;
//                break;
//            case AllCode.YouPin:
//                mType = 4;
//                break;
//            case AllCode.College:
//                mType = 5;
//                break;
//            case AllCode.StarStore:
//                mType = 6;
//                break;
//            case Type.GoodsType.GiveMore:
//                mType = 7;
//                break;
//            case Type.GoodsType.FreeGift:
//                mType = 8;
//                break;
//            case Type.GoodsType.JingDong:
//                mType = 9;
//                break;
//        }

        initRcy();
        initRefreshLayout();
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        L.i("订单列表收到：" + eventMessage.getEventType() + "   " + eventMessage.getGoodsType());
        if (AllCode.UPDATE_DATA.equals(eventMessage.getEventType())) {//只刷新当前界面数据
            if (eventMessage.getGoodsType() == mType) {
                mSmartRefreshLayout.autoRefresh();
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (isAdded()) {
            currentPage = 1;
            if (mType == 1 && key == 2) {
                getP().queryOfflineList();
            } else {
                getP().queryOrderList(currentPage, key, mType);
            }
        }
    }

    private void initRefreshLayout() {
        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 1;
            if (mType == 1 && key == 2) {
                getP().queryOfflineList();
            } else {
                getP().queryOrderList(currentPage, key, mType);
            }

        });
        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage++;
            getP().queryOrderList(currentPage, key, mType);
        });
        mSmartRefreshLayout.autoRefresh();
    }

    private void initRcy() {
        mRecyclerView.setPadding(DensityUtil.dp2px(12), DensityUtil.dp2px(10), DensityUtil.dp2px(12), 0);
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
//        if (mType == 3) {
//            mRecyclerView.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10), false));
//            orderAdapter = new BaoPinOrderAdapter(null);
//            ((BaoPinOrderAdapter) orderAdapter).setOnParentChildClickListener((adapter, view, childPosition, parentPosition) -> {
//                OrderInfo orderInfo = orderAdapter.getData().get(parentPosition);
//                if (key == Constant.CODE_GET_ORDER_INFO_REQUEST_ID) {//发布圈子时返回的订单信息
//                    List<OrderGoods> orderGoods = ((BaoPinOrderGoodsAdapter) adapter).getData();
//                    OrderGoods goods = orderGoods.get(childPosition);
//                    OrderActivity orderActivity = (OrderActivity) getActivity();
//                    Intent intent = new Intent();
//                    intent.putExtra(Constant.KEY_ORDER_ID, String.valueOf(goods.getOrder_id()));
//                    intent.putExtra(Constant.KEY_PRODUCT_ID, String.valueOf(goods.getProduct_id()));
////                    intent.putExtra(Constant.KEY_ORDER_TYPE,mType);
//                    intent.putExtra(Constant.KEY_ORDER_TYPE, goods.getOrder_type());
//                    intent.putExtra(Constant.KEY_TITLE, goods.getItem_title());
//                    orderActivity.setResult(Constant.CODE_GET_ORDER_INFO_RESULT_ID, intent);
//                    orderActivity.finish();
//                } else {
//                    if (mType != 1) {
//                        Router.newIntent(context)
//                                .putString(Constant.ID, String.valueOf(orderInfo.getId()))
//                                .putInt(Constant.TYPE, orderInfo.getOrder_type())
//                                .putInt(Constant.KEY,mType)
//                                .to(OrderInfoActivity.class)
//                                .launch();
//                    }
//                }
//                return Unit.INSTANCE;
//            });
//        } else if(mType==9){//京东
//            mRecyclerView.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10), false));
//            orderAdapter = new JDOrderAdapter();
//        }else {
//
//        }
        orderAdapter = new OrderAdapter(null, key, mType);
        mRecyclerView.setAdapter(orderAdapter);
        orderAdapter.setOnItemClickListener((adapter, view, position) -> {
            OrderInfo orderInfo = orderAdapter.getData().get(position);
            if (key == Constant.CODE_GET_ORDER_INFO_REQUEST_ID) {//发布圈子时返回的订单信息
                OrderActivity orderActivity = (OrderActivity) getActivity();
                Intent intent = new Intent();
                intent.putExtra(Constant.KEY_ORDER_ID, String.valueOf(orderInfo.getOrder_id()));
                intent.putExtra(Constant.KEY_PRODUCT_ID, orderInfo.getProduct_id());
                intent.putExtra(Constant.KEY_ORDER_TYPE, orderInfo.getOrder_type());
                intent.putExtra(Constant.KEY_TITLE, orderInfo.getItem_title());
                orderActivity.setResult(Constant.CODE_GET_ORDER_INFO_RESULT_ID, intent);
                orderActivity.finish();
            } else {
                if (mType != 1 && mType != 2) {
                    Router.newIntent(context)
                            .putString(Constant.ID, String.valueOf(orderInfo.getId()))
                            .putInt(Constant.TYPE, orderInfo.getOrder_type())
                            .putInt(Constant.KEY, mType)
                            .to(OrderInfoActivity.class)
                            .launch();
                }
            }
        });
        orderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        orderAdapter.setOnChoose(new OrderAdapter.OnClick() {
            @Override
            public void OnChoose(int type, String id) {
                if (type == 1) {
                    if (rejectDialog == null) {
                        rejectDialog = new RejectDialog(context, new RejectDialog.OnCloseListener() {
                            @Override
                            public void confirm(String remark) {
                                ExamineBean data = new ExamineBean();
                                data.id = id;
                                data.remark = remark;
                                data.status = 1;
                                getP().offExamine(data);
                            }
                        });
                    }
                    rejectDialog.show();
                } else {
                    accpetDialog = new AccpetDialog(context, new AccpetDialog.OnCloseListener() {
                        @Override
                        public void confirm(int type) {
                            ExamineBean1 data = new ExamineBean1();
                            data.id = id;
                            data.type = type;
                            data.status = 2;
                            getP().offExamine1(data);
                        }
                    });
                    accpetDialog.show();
                }
            }
        });
    }

    public void setData1(List<OrderInfo> orderList) {
        isRefresh = true;
        if (currentPage == 1) {
            if (orderList == null || orderList.size() == 0) {
//                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refresh = false;
                mStatusManager.showContent();
                orderAdapter.setNewData(orderList);
            }
            mSmartRefreshLayout.finishRefresh(true);
        } else {
            if (orderList == null || orderList.size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                orderAdapter.addData(orderList);
            }
            mSmartRefreshLayout.finishLoadMore(true);
        }
        getP().queryOrderList(currentPage, key, mType);
    }
    private boolean refresh = false;

    public void setData(OrderList orderList) {
        isRefresh = true;
        if (currentPage == 1) {
            if (orderList.getData() == null || orderList.getData().size() == 0) {
                if (mSmartRefreshLayout != null) {
                    mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
                if (mType == 1 && key == 2) {
                    if(!refresh){
                        if(orderAdapter.getData().size() == 0){
                            mStatusManager.showEmpty();
                        }else{
                            orderAdapter.addData(orderList.getData());
                        }
                        refresh = true;
                    }
                } else {
                    mStatusManager.showEmpty();
                    orderAdapter.setNewData(orderList.getData());
                }

            } else {
                mStatusManager.showContent();
                if (mType == 1 && key == 2) {
                    if(!refresh){
                        if(orderAdapter.getData().size() == 0){
                            orderAdapter.setNewData(orderList.getData());
                        }else{
                            orderAdapter.addData(orderList.getData());
                        }
                        refresh = true;
                    }
                } else {
                    orderAdapter.setNewData(orderList.getData());
                }
            }
            if (mSmartRefreshLayout != null) {
                mSmartRefreshLayout.finishRefresh(true);
                if (orderList.getTotal() <= orderList.getData().size()) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                } else {
                    mSmartRefreshLayout.setEnableLoadMore(true);
                }
            }

        } else {
            if (orderList.getData() == null || orderList.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                if (orderList.getData().size() <= 10) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                }
                orderAdapter.addData(orderList.getData());
            }
            mSmartRefreshLayout.finishLoadMore(true);
        }
        currentPage = orderList.getCurrent_page();
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }

    void setData() {
        refresh = false;
        orderAdapter.getData().clear();
        currentPage = 1;
        if (mType == 1 && key == 2) {
            getP().queryOfflineList();
        } else {
            getP().queryOrderList(currentPage, key, mType);
        }
    }

}
